<?php

namespace addons\cardocr\controller;

use addons\cardocr\model\CardocrLog;
use app\common\model\User;
use think\Config;
use think\addons\Controller;
use addons\cardocr\library\Card;
use addons\cardocr\model\Cardocr;
use think\Exception;

class Index extends Controller
{
    protected $noNeedRight = ['index'];

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if (!$this->auth->isLogin()) {
            $this->error(__('Please login first'));
        }
        if (!$this->request->isPost()) {
            $this->error("无效请求");
        }


        $card_config = get_addon_config('cardocr');
        $row = $this->request->post();

        //人工审核,不请求api
        if (!$card_config['isapi']) {
            try {
                $inster_data = array();
                $inster_data["user_id"] = (int)$this->auth->id;
                $inster_data["name"] = $row['username'];
                $inster_data["idnum"] = $row['idnum'];
                $inster_data["sex"] = !empty($row['sex']) ? $row['sex'] : "未知";
                $inster_data['nation'] = "";
                $inster_data['birth'] = date("Ymd");
                $inster_data['address'] = "";
                $inster_data['authority'] = "";
                $inster_data['validdatestart'] = date("Ymd");
                $inster_data['validdateend'] = date("Ymd");
                $inster_data['positive_img'] = $row['positive_img'];
                $inster_data['back_img'] = $row['back_img'];
                $inster_data['status'] = "2";
                Cardocr::create($inster_data);
                $arr["code"] = 1;
                $arr["result"] = 0;
                $arr["time"] = time();
                $arr["msg"] = "提交成功";
                $arr["data"] = "";
                echo json_encode($arr);
                die();

            } catch (Exception $e) {
                $this->error($e->getMessage());
            }

        }

        //限制api访问次数
        $userid = $this->auth->id;

        $replay = CardocrLog::where(array("user_id" => $userid))->whereTime('createtime', 'today')->count();
        if (intval($card_config['replay']) <= $replay) {
            $this->error("单日审核次数已满");
        }

        $url = Config::get('upload.cdnurl');


        $name = $row['username'];
        $idnum = $row['idnum'];


        $positive_img = cdnurl($row['positive_img'], true);
        $back_img = cdnurl($row['back_img'], true);

        $data = \addons\cardocr\model\Cardocr::whereOr('user_id', '=', $this->auth->id)->whereOr(
            'idnum',
            '=',
            $idnum
        )->find();

        if (!isset($data) || $data->status != 1) {
        } else {
            $this->error("身份证已经验证通过");
        }

        //记录api记录
        CardocrLog::record("身份证核验");

        $cardocr = new \app\index\controller\Cardocr();
        //ocr识别身份证正面

        $orc_frontdata = $cardocr->checkfront($positive_img);

        if ($orc_frontdata['Name'] != $name && $orc_frontdata['IdNum'] != $idnum) {
            $this->error("身份证图片与你输入的姓名与身份证号码不一致");
        }

        //ocr识别身份证反面
        $orc_backdata = $cardocr->checkback($back_img);

        //获取腾讯云身份验证api
        $params_data = Card::checkidcard($idnum, $name);

        try {
            if ($params_data['Result'] == 0) {
                $inster_data = array();
                $inster_data["user_id"] = (int)$this->auth->id;
                $inster_data["name"] = $name;
                $inster_data["idnum"] = $idnum;
                $inster_data["sex"] = !empty($orc_frontdata['Sex']) ? $orc_frontdata['Sex'] : "未知";
                $inster_data['nation'] = !empty($orc_frontdata['Nation']) ? $orc_frontdata['Nation'] : "";
                $inster_data['birth'] = !empty($orc_frontdata['Birth']) ? $orc_frontdata['Birth'] : null;
                $inster_data['address'] = !empty($orc_frontdata['Address']) ? $orc_frontdata['Address'] : "";
                $inster_data['authority'] = !empty($orc_backdata['backdata']['Authority']) ? $orc_backdata['backdata']['Authority'] : "";
                $inster_data['validdatestart'] = $orc_backdata['validdatestart'];
                $inster_data['validdateend'] = $orc_backdata['validdateend'];
                $inster_data['positive_img'] = $row['positive_img'];
                $inster_data['back_img'] = $row['back_img'];
                $inster_data['status'] = "1";
                Cardocr::create($inster_data);
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }


        $arr["code"] = 1;
        $arr["result"] = $params_data['Result'];
        $arr["time"] = time();
        $arr["msg"] = $params_data['Description'];
        $arr["data"] = $params_data;

        echo json_encode($arr);
        die();
    }
}
